package com.pig4cloud.pig.module.controller.estate;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.pig4cloud.pig.common.core.util.R;
import com.pig4cloud.pig.common.log.annotation.SysLog;
import com.pig4cloud.pig.common.mybatis.page.PageUtils;
import com.pig4cloud.pig.common.mybatis.page.TableDataInfo;
import com.pig4cloud.pig.module.api.entity.estate.EstateRoom;
import com.pig4cloud.pig.module.api.entity.excel.PropertyWaterTemplateExcel;
import com.pig4cloud.pig.module.api.entity.property.PropertyFeeOrderTemplate;
import com.pig4cloud.pig.module.service.estate.IEstateRoomService;
import com.pig4cloud.pig.module.service.property.IPropertyFeeOrderTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import lombok.RequiredArgsConstructor;
import lombok.extern.java.Log;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpHeaders;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 小区内的房屋列信息Controller
 *
 * @author ruoyi
 * @date 2022-07-08
 */
@Validated
@Api(value = "小区内的房屋列信息控制器", tags = {"小区内的房屋列信息管理"})
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/estate/room")
@SecurityRequirement(name = HttpHeaders.AUTHORIZATION)
public class EstateRoomController  {

    private final IEstateRoomService estateRoomService;
	private final IPropertyFeeOrderTemplateService propertyFeeOrderTemplateService;


    /**
     * 查询小区内的房屋列信息列表
     */
    @ApiOperation("查询小区内的房屋列信息列表")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:list')")
    @GetMapping("/list")
    public TableDataInfo<EstateRoom> list(@Validated EstateRoom estateRoom) {
        QueryWrapper<EstateRoom> qw = new QueryWrapper<>();
        if(estateRoom.getEstateId()!=null){
        	qw.eq("estate_id",estateRoom.getEstateId());
		}
        if(estateRoom.getRoomNum()!=null){
            qw.like("room_num",estateRoom.getRoomNum());
        }
        if(estateRoom.getUnitNo()!=null){
            qw.like("unit_no",estateRoom.getUnitNo());
        }
        if(estateRoom.getBuildNo()!=null){
            qw.like("build_no",estateRoom.getBuildNo());
        }
//		if(estateRoom.getRoomNo()!=null){
//			qw.and(wrapper->{
//				wrapper.like("room_no", estateRoom.getRoomNo());
//			});
//		}

		if(estateRoom.getRealName()!=null){
			qw.and(wrapper->{
				wrapper.like("real_name", estateRoom.getRealName())
					.or().like("phone_num",estateRoom.getRealName())
					.or().like("room_no", estateRoom.getRealName());
			});
		}
		qw.orderByAsc("room_no");
        TableDataInfo<EstateRoom> td = PageUtils.buildDataInfo(estateRoomService.page(PageUtils.buildPage(),qw));
        List<EstateRoom> list = td.getData();
        /*if(estateRoom.getQianfei()==0){*/
           /* QueryWrapper<PropertyFeeOrderTemplate> qw1=new QueryWrapper<>();
            qw1.eq("year_num",2022).or()
                    .le("month_num",9).or().eq("state",0);
            List<PropertyFeeOrderTemplate> list1=propertyFeeOrderTemplateService.list(qw1);
            for  (int i = 0;i < list1.size()- 1;i++)  {
                for  (int j = list1.size() - 1;j > i;j--)  {
                    if  (list1.get(j).getRoomNo().equals(list1.get(i).getRoomNo()))  {
                        list1.remove(j);
                    }
                }
            }
            for  (int i = 0;i < list1.size()- 1;i++)  {
                for  (int j = list.size() - 1;j > i;j--)  {
                    if  (list.get(j).getRoomNo().equals(list1.get(i).getRoomNo()))  {
                        list.remove(j);
                    }
                }
            }*/
        /*}*/
        /*System.out.println(estateRoom.getQianfei()==0);*/
        return td;
    }

    /**
     * 导出小区内的房屋列信息列表
     */
    @ApiOperation("导出小区内的房屋列信息列表")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:export')")
    @SysLog("小区内的房屋列信息")
    @GetMapping("/export")
    public List export(@Validated EstateRoom estateRoom) {
        List<EstateRoom> list = estateRoomService.list();
//        // ExcelUtil<EstateRoom> util = new // ExcelUtil<EstateRoom>(EstateRoom.class);
        return list;// util.exportExcel(list, "小区内的房屋列信息");
    }

    /**
     * 导出小区内的水表底数列表
     */
    @ApiOperation("导出小区内的房屋列信息列表")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:export')")
    @SysLog("小区内的房屋列信息")
    @GetMapping("/waterExport")
    public List waterExport(@Validated EstateRoom estateRoom) {
        QueryWrapper<PropertyFeeOrderTemplate> qw=new QueryWrapper<>();
        qw.eq("estate_id",estateRoom.getEstateId());
//        qw.eq("is_manual_data",1);
//        qw.eq("year_num",2022);
        List<PropertyFeeOrderTemplate> list1 = propertyFeeOrderTemplateService.list(qw);
        QueryWrapper<EstateRoom> qw1=new QueryWrapper<>();
        qw1.eq("id",estateRoom.getEstateId());
        List<EstateRoom> list = estateRoomService.list(qw1);
        List<PropertyWaterTemplateExcel> waterList = new ArrayList<>();
        PropertyWaterTemplateExcel property =new PropertyWaterTemplateExcel();
        for (EstateRoom list2:list){
            for (PropertyFeeOrderTemplate list3:list1){
                if (list3.getRoomId().equals(list2.getId())){
                    property.setRoomNo(list2.getRoomNo());
                    if (list3.getMonthNum().equals("1")){
                        property.setLastYear(list3.getBeginCount());
                        property.setJanuary(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("2")){
                        property.setFebruary(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("3")){
                        property.setMarch(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("4")){
                        property.setApril(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("5")){
                        property.setMay(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("6")){
                        property.setJune(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("7")){
                        property.setJuly(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("8")){
                        property.setAugust(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("9")){
                        property.setSeptember(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("10")){
                        property.setOctober(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("11")){
                        property.setNovember(list3.getEndCount());
                    }
                    if (list3.getMonthNum().equals("12")){
                        property.setDecember(list3.getEndCount());
                    }
                }
            }
            waterList.add(property);
        }
//        // ExcelUtil<PropertyWaterTemplateExcel> util = new // ExcelUtil<PropertyWaterTemplateExcel>(PropertyWaterTemplateExcel.class);
        return waterList;//util.exportExcel(waterList, "小区内的水表信息");
    }

    /**
     * 获取小区内的房屋列信息详细信息
     */
    @ApiOperation("获取小区内的房屋列信息详细信息")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:query')")
    @GetMapping("/{id}")
    public R<EstateRoom> getInfo(@NotNull(message = "主键不能为空") @PathVariable("id") Long id) {
        EstateRoom estateRoom = estateRoomService.getById(id);
        return R.ok(estateRoom);
    }

    /**
     * 新增小区内的房屋列信息
     */
    @ApiOperation("新增小区内的房屋列信息")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:add')")
    @SysLog("小区内的房屋列信息")
    @PostMapping()
    public R<Void> add(@Valid @RequestBody EstateRoom estateRoom) {
        return R.okByRow(estateRoomService.save(estateRoom) ? 1 : 0);
    }

    /**
     * 修改小区内的房屋列信息
     */
    @ApiOperation("修改小区内的房屋列信息")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:edit')")
    @SysLog("小区内的房屋列信息")
    @PutMapping()
    public R<Void> edit(@Valid @RequestBody EstateRoom estateRoom) {
    	int ret = estateRoomService.updateRoom(estateRoom);
        return R.okByRow(ret);
    }

    /**
     * 删除小区内的房屋列信息
     */
    @ApiOperation("删除小区内的房屋列信息")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:remove')")
    @SysLog("小区内的房屋列信息")
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")  @PathVariable Long[] ids) {
        return R.okByRow(estateRoomService.removeByIds(Arrays.asList(ids)) ? 1 : 0);
    }
    /**
     * 通过id查询房屋信息
     */
    public List<EstateRoom> roomList(Integer id) {
        QueryWrapper<EstateRoom> qw=new QueryWrapper<>();
        qw.eq("id",id);
        List<EstateRoom> list=estateRoomService.list(qw);
        return list;
    }

    /**
     * 通过房屋信息查询id
     */
    public List<EstateRoom> roomInfo(Integer estateId,String buildNo,String unitNo,String roomNum) {
        QueryWrapper<EstateRoom> qw=new QueryWrapper<>();
        qw.eq("estate_id",estateId);
        qw.eq("build_no",buildNo);
        qw.eq("unit_no",unitNo);
        qw.eq("room_num",roomNum);
        List<EstateRoom> list=estateRoomService.list(qw);
        return list;
    }

    @ApiOperation("查询小区内本年度欠费的的房屋列信息列表")
    @PreAuthorize("@pms.hasPermission('module:estateRoom:list')")
    @GetMapping("/arrearageList")
    public TableDataInfo<EstateRoom> arrearageList(@Validated EstateRoom estateRoom) {
        QueryWrapper<PropertyFeeOrderTemplate> qw1=new QueryWrapper<>();
        if (estateRoom.getYear()!=null){
            qw1.eq("year_num",estateRoom.getYear());
        }
        if (estateRoom.getEstateId()!=null){
            qw1.eq("estate_id",estateRoom.getEstateId());
        }
        qw1.groupBy("room_id");
        List<PropertyFeeOrderTemplate> list1 = propertyFeeOrderTemplateService.list(qw1);
        List<EstateRoom> roomList = new ArrayList<>();
        for (PropertyFeeOrderTemplate list:list1){
            EstateRoom room=estateRoomService.selectByUniqueColumn("id",list.getRoomId());
            roomList.add(room);
        }
        return PageUtils.buildDataInfo(roomList);
    }
}
